Repository navigation
Run a stage agent as the person who moved the post in, and let a failed run go again in place - #168
Conversation
7375e02 to
d0045e2
Compare
There was a problem hiding this comment.
-
P1: Re-entry retains the previous mover’s authority.
resolve_run_as_user()(class-stage-agent-runner.php:867) now trustsstarted_by, but the pending-job guard preserves it across re-entry.An editor moved a contributor’s post into the AI stage, then confirmed a move out before execution. The contributor changed the content and moved it back. The agent still ran as the editor. With
allow_agent_publish=true, it published the contributor’s new content despite the contributor lackingpublish_posts.Refresh the job identity and origin on each actual stage entry. Deduplicate the cron event without retaining authorization from an abandoned entry.
-
P2: Run again bypasses sequence role restrictions.
rerun_agent_stage()(class-workflow-controller.php:869) requiresedit_postbut does not enforce an applicable sequence permission.An editor-only entry rejected the contributor with forbidden_transition. After an editor entered the stage and the run failed, the contributor’s retry returned HTTP 200. The agent ran as the contributor and advanced the post. The contributor could also trigger this through the panel.
SPEC.mdpromises sequence transition rules for this route.Enforce an explicit retry permission that preserves the sequence’s role restrictions. Apply the same check to
can_rerun. -
P2: Run again and Go back can both succeed. The conditional update in
rerun()(class-stage-agent-runner.php:326) does not coordinate withrevert_failed_agent_stage()(class-status-manager.php:275).I paused Go back immediately before its stage write and executed a retry through REST. Both requests returned HTTP 200. The post ended in Drafting with a pending job for the AI stage. That retry cannot execute at its requested stage.
Make both operations conditionally claim the failed job before changing state. Return a conflict to the losing request.
|
Thanks, Chris, all three were real, and fixing them turned up two more.
SPEC.md's stage-agent section is updated to match. One thing I noticed and left alone, because it predates this PR: a Go back that a soft-warning tool holds comes back to the editor as a plain success. Filed as #193. |
A stage agent ran as the post author and nobody else. When an editor moved someone else's post into an AI stage and the author could not edit posts, the run stopped in place, and moving the post back and in again failed the same way: the editor's identity was recorded on the job as `started_by` and then used only for telemetry. The run now acts as that person when they can still edit this post, re-checked when the run fires, and falls back to the author otherwise. A run nobody started (cron, a scheduled publish, a reseat with no one logged in) still has only the author to act as, and still refuses rather than borrow anyone. Revisions the agent writes are therefore authored by whoever moved the post in, and still tagged with the agent. The no-actor message now says who has to move the post for the agent to run. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A failed AI stage offered one action, Go back, so retrying meant moving the
post out of its stage and back in. Now that a run acts as the person who
started it, a retry started by whoever is looking at the failure fixes the
failures a different identity can fix, so it no longer needs the round trip.
POST /workflow/post/{id}/agent-rerun dispatches a fresh run for a post whose
agent failed at its current stage, started by the current user. The post
stays put, the go-back origin is kept, and the loop-guard chain restarts.
The failed job payload carries can_rerun, and the panel and header lead with
Run again when it is set; Go back stays beside it in the panel.
The no-actor failure now says why the run stopped rather than what to do,
since the action is on the button: either the post moved in automatically
and its author can't edit posts, or the person who moved it here has lost
the right to edit it, and so has its author.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The header drew its button only when there was a move to offer. While a stage agent runs, the stage withholds its exits, so the button vanished between the press that started the run and the outcome: Run again showed busy, disappeared, then came back. Moving a post into an AI stage did the same. While a run is pending, the main half now stays as a busy, disabled "Agent working" button until the outcome arrives. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…onal An agent-to-agent hop took its starter from get_current_user_id() first and fell back to the chain's starter only when nobody was logged in. A cron run with an ambient user (wp cron event run --user=…, or a user an earlier callback left set) made that user the next stage's starter, and the run then acted with their rights. While an exit transition is in progress, the carried starter now always wins, and a chain nobody started stays nobody's. Run again replaced the failed marker unconditionally, so two people pressing it at once, or Run again racing Go back, could both succeed. The pending marker now replaces the failed one only if it is still exactly the marker that was checked; the loser gets 409 agent_rerun_conflict and schedules nothing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…are-and-swap The starter an agent exit carries was process-wide while the exit ran, so a callback inside that transition that moved a different post into an AI stage gave that post the exiting run's starter. It is now keyed by post and applies only to the exiting post's own next stage. Separately, a dispatch under cron no longer takes the logged-in user at all: nobody moved the post there, whoever cron runs as. Run again's conditional write used update_post_meta()'s $prev_value, which adds the row when none exists. A Run again that read the failed marker before a Go back deleted it could therefore still create a marker and queue a run. The replace is now one UPDATE whose WHERE names the exact failed marker; it never adds a row, so the stale request is refused with agent_rerun_conflict and queues nothing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A refused Run again (409 agent_rerun_conflict when someone else ran it first or took it back, or no_agent_rerun) reported the error but kept the stale failed state on screen, so the panel and header went on offering a Run again that could not succeed. The store action now re-reads the post's status before rejecting, so both surfaces show what the post allows now. Tests cover the refusal in the header (error notice, then the re-read state) and the panel (the error, the re-read, Run again gone), and the panel's success test now checks the failure and its actions leave. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Run again posted straight to /agent-rerun, so the agent read the stored post and never saw what was on screen. That is the likeliest moment for the editor to be dirty: the natural response to a failed run is to fix the copy and press Run again. The agent then worked from the old text, and when it routed the post on, the author's edits were lost on reload. Run again now goes through the transition flow (requestRerun), which saves first on exactly the terms a move into an AI stage uses: it waits out a save in flight, asks what decideSave() asks, saves, and stops if the save did not take. The wait-decide-save-verify sequence is one helper, saveFirst(), shared by moves and Run again. Like entering an AI stage, Run again is refused while another editor has the post open. Also: a failed compare-and-swap query is now logged as a database error rather than passing silently as a conflict, and comments in the panel, the stylesheet and the agent-stages e2e spec no longer describe the failed state as having only Go back. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The spec said a stage agent runs as the post author only, that started_by
is used only for telemetry, and that a failed run offers only Go back.
It now says a run acts as whoever started it when they can still edit the
post, falling back to the author for runs nobody started; that a failed
run offers Run again (POST /workflow/post/{id}/agent-rerun) beside Go
back; and that Run again restarts the loop-guard chain. The footgun about
screens saying "Re-run" with no re-run action is removed.
This replaces the change this branch made to transition-rail.md, which
main has since deleted.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Every entry into an AI stage writes a fresh job marker, even over a pending one left by an entry a person abandoned. The run acts as the person who made this entry, and Go back leads where this entry came from. Only the cron event is still deduplicated. - Run again requires the sequence's role rules to allow the move that entered the stage (from the job's origin, or any move in when it has none). Otherwise 403 forbidden_transition, and can_rerun is false. - Go back takes the failed marker by compare-and-delete before it moves the post, so it and Run again can't both succeed. The loser gets 409 agent_revert_conflict; a refused move puts the failed marker back. The editor re-reads the post when Go back is refused, as it already did for Run again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
From the review of the previous commit: - A marker for the run's stage that has already resolved (failed, or held for warnings) has no run left to serve, so a second cron event for the same post and stage returns without executing the agent. Cron only deduplicates events about ten minutes apart, inside the 15-minute pending window. - A reseat records the stage the post sat in, which has no authored edge into the AI stage, so nobody could run it again. When the recorded origin isn't an authored edge, any authored move in decides; a stage no edge leads into has no role rule to keep; the bypass list skips the check, as everywhere. - The compare-and-swap and compare-and-delete share one helper. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
From the second review pass: - Go back swapped the failed marker out by deleting it before the move, leaving the post at the AI stage with no marker for the length of the request; a late run event fired then would run the agent. It now swaps the marker for a `reverting` one, which the run guard treats as resolved, then clears it after the move or swaps `failed` back if the move doesn't happen (refused, or held for soft warnings). - The Go back conflict test checks the refusal is shown. - SPEC.md says a stage no authored move leads into has no Run again rule. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
0f05314 to
18b6f81
Compare
alecgeatches
left a comment
There was a problem hiding this comment.
Chris's three points above look fixed to me. One thing on top of those:
A crash during Go back can leave the post stuck in reverting
revert_failed_agent_stage() changes the marker from failed to reverting, then calls transition(). The marker is only put back when transition() returns something other than true:
$result = $this->transition( $post_id, $revert_to, array( 'agent_revert' => true ) );
if ( true !== $result ) {
StageAgentRunner::take_job_marker( $post_id, $reverting, $failed_job );
return $result;
}If transition() throws, or the request dies before the stage is written (a fatal in a hooked callback, a timeout), nothing restores the marker. The post stays in the AI stage with status: reverting, and from there:
- Run again refuses, because
rerun()needsfailed. - Go back refuses, because
get_agent_revert_stage()needsfailed. - Nothing expires it. The timeout only covers
pending.
The only way out is one of the stage's normal moves. If the stage has none the person can take, someone has to fix the post meta by hand.
A try/finally that restores the failed marker would cover the exception case. Treating an old reverting marker as failed would also cover a request that dies outright. A test for the throwing case would be good, since I don't see one for this path.
Go back swaps the failed marker for a `reverting` one, moves the post, and swaps it back if the move doesn't happen. If transition() threw, or the request died before the move finished, nothing put it back: Run again and Go back both need `failed`, and only `pending` times out, so the post sat in the AI stage with neither on offer. - A throw from transition() puts the failed marker back by compare-and-swap, then rethrows. - The reverting marker is stamped with `reverting_at`. Read through StageAgentRunner::read_job_marker(), one older than REVERTING_TTL (five minutes) is swapped back to the failed marker, so a request that died outright recovers too. Go back's origin, Run again and the status payload all read through it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks, good catch. Fixed in e76cd6a, taking both of your suggestions. A throw from |
Intent
A stage agent always ran as the post's author. When the author couldn't edit posts (a subscriber, a removed account, an imported author), every run stopped with "The author cannot edit posts", and the advice to move the post back and forward again couldn't help: the retry ran as the same author and failed the same way. An editor who could plainly do the work had no way to have the agent do it.
This PR makes a run act as the person who started it, when they can still edit the post. It also adds a direct retry, Run again, so fixing a failed run no longer takes a trip out of the stage and back in.
Spec changes
agent-rerun.agent_rerun_conflict). The state diagram gains the Failed → Pending edge.edit_postwhen the run fires. A run nobody started still acts only as the author, and only if the author hasedit_posts. The line sayingstarted_byis used only for telemetry is gone, becausestarted_bynow decides who the run acts as. The exit transition and the agent-publish check are authorized against the user the run acts as. Run again resets the loop-guard counter.What was done
Who a run acts as
StageAgentRunner::resolve_run_as_user()picks who a run acts as. It returns the person who started the run (started_byin the job marker) ifuser_can( $started_by, 'edit_post', $post_id )is true when the run fires. Otherwise it falls back toresolve_agent_user(), the author check, unchanged.wp cron event run --user=…). Their rights are re-checked on every hop.Run again
POST /vip-workflows/v1/workflow/post/{id}/agent-rerun(WorkflowController::rerun_agent_stage(),StageAgentRunner::rerun()). It dispatches a fresh run for a post whose agent failed at its current stage. The run is started by the current user, so it acts as them. The post stays put, the go-back origin is kept, and the loop-guard chain restarts.409 no_agent_rerunwhen there's no failed run at the current stage, or when the stage no longer has an agent.409 agent_rerun_conflictand nothing is written or scheduled. The replace is a single conditional UPDATE that never creates a row.agent_jobin the status payload gainscan_rerun.requestRerun()), which waits out a save in flight, asks whatdecideSave()asks, saves, and stops if the save didn't take. That wait-decide-save-check sequence is now one helper,saveFirst(), shared by moves and Run again. Like entering an AI stage, Run again is refused while another editor has the post open.dispatch(), used by both entry and re-run.API changes for callers
POST /workflow/post/{id}/agent-rerun.agent_job.can_rerunon failed jobs.agent_job.causecan now bererun.Also in this PR: header button stays on screen while an agent runs
This is separate from the fix above and is in its own commit (
Keep the header button on screen while a stage agent runs).The header's split button was drawn only when there was a move to offer. While a stage agent runs, the stage holds back its exits, so the button disappeared between the press that started the run and the outcome. Run again showed busy, vanished, then came back. Moving a post into an AI stage did the same. While a run is pending, the main half now stays as a busy, disabled Agent working button until the outcome arrives. Only
WorkflowHeaderAction.jsand its test change.Screenshots
Our button is gone.
How to test
The preview has no AI provider configured, so a real agent can't run in it. A failed run also needs a seeded job marker. The quickest full check is on wp-env with a stub agent. Adding a stub agent and a failed-run post to
.playground/blueprint.jsonwould make this testable in the preview; that's a possible follow-up, not done here.npm run build(root) compiles clean.cd vip-workflows && composer test: unit tests pass (2,031, 1 skipped).npm --prefix vip-workflows run test:unit:js: JS unit tests pass (1,869). They include Run again saving unsaved edits before it runs, stopping when that save fails, and its refusal path in the header and the panel.npm run test:php:integration: integration tests pass (580). They includeAgentRunsAsStarterIntegrationTest, which covers an editor's move on a subscriber's post, a demoted starter falling back to the author, a run nobody started refusing in place, and Run again by an editor fixing that refusal through the REST route.started_by0. Run the agent with nobody logged in (wp evalcallingStageAgentRunner::run_stage_agent()afterwp_set_current_user( 0 )). Expect it to fail with "This post moved here automatically…".wp cron event run --due-nowin a loop). The header button stays busy as Agent working for the whole run. The post moves on to Review, and the run is credited to you.The preview can show the header and panel layout for any post in a workflow, but not the failed or running agent states.
Follow-ups
🤖 Generated with Claude Code